///PHILOTES Source Code.  (C)2012 PhiloLabs

#include "stdneb.h"
#include "logManager.h"
#include "exception.h"
#include <algorithm>

namespace Core {
   
	using namespace Util;

	template<> LogManager* Singleton<LogManager>::msSingleton = 0;

	LogManager* LogManager::Instance()
	{ 
		ph_assert( msSingleton );
		return msSingleton; 
	}

	bool LogManager::HasInstance()
	{
		return 0 != msSingleton; 
	}

    //-----------------------------------------------------------------------
    LogManager::LogManager()
    {
		mDefaultLog = NULL;
    }
    //-----------------------------------------------------------------------
    LogManager::~LogManager()
    {
        // Destroy all logs
        SizeT size = mLogs.Size();
        for (SizeT i = 0; i < size; ++i)
        {
            delete mLogs.ValueAtIndex(i);
        }
    }
    //-----------------------------------------------------------------------
    Log* LogManager::createLog( const String& name, bool defaultLog, bool debuggerOutput, 
		bool suppressFileOutput)
    {
        Log* newLog = ph_new( Log(name, debuggerOutput, suppressFileOutput) );

        if( !mDefaultLog || defaultLog )
            mDefaultLog = newLog;

		mLogs.Add(name,newLog);

        return newLog;
    }
    //-----------------------------------------------------------------------
    Log* LogManager::getDefaultLog()
    {	
        return mDefaultLog;
    }
    //-----------------------------------------------------------------------
    Log* LogManager::setDefaultLog(Log* newLog)
    {
        Log* oldLog = mDefaultLog;
        mDefaultLog = newLog;
        return oldLog;
    }
    //-----------------------------------------------------------------------
    Log* LogManager::getLog( const String& name)
    {
		IndexT i = mLogs.FindIndex(name);
        if (i != InvalidIndex)
            return mLogs.ValueAtIndex(i);
        else
            PHILO_EXCEPT(Exception::ERR_INVALIDPARAMS, "Log not found. ", "LogManager::getLog");
    }
	//-----------------------------------------------------------------------
	void LogManager::destroyLog(const String& name)
	{
		IndexT i = mLogs.FindIndex(name);
		if (i != InvalidIndex)
		{
			if (mDefaultLog == mLogs.ValueAtIndex(i))
				mDefaultLog = 0;

			ph_delete( mLogs.ValueAtIndex(i) );
			mLogs.EraseAtIndex(i);
		}

		// Set another default log if this one removed
		if (!mDefaultLog && !mLogs.IsEmpty())
			mDefaultLog = mLogs.ValueAtIndex(0);
	}
	//-----------------------------------------------------------------------
	void LogManager::destroyLog(Log* log)
	{
		destroyLog(log->getName());
	}
    //-----------------------------------------------------------------------
    void LogManager::logMessage( const String& message, LogMessageLevel lml, bool maskDebug)
    {
		if (mDefaultLog)
			mDefaultLog->logMessage(message, lml, maskDebug);
    }
    //-----------------------------------------------------------------------
    void LogManager::setLogDetail(LoggingLevel ll)
    {
		if (mDefaultLog)
	        mDefaultLog->setLogDetail(ll);
    }
	//---------------------------------------------------------------------
	Log::Stream LogManager::stream(LogMessageLevel lml, bool maskDebug)
	{
		if (mDefaultLog)
			return mDefaultLog->stream(lml, maskDebug);
		else
			PHILO_EXCEPT(Exception::ERR_INVALIDPARAMS, "Default log not found. ", "LogManager::stream");
	}

}//namespace Core
